package game.adm.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import game.adm.dao.BetTypeDAO;
import game.adm.dao.dto.BetTypeDTO;
import projectx.core.base.ProjectXSystemException;
import projectx.core.dao.AbstractDAO;
import projectx.core.dao.DataAccessException;
import projectx.core.dao.ExceptionHandler;
import projectx.core.dao.impl.PreparedStatementBuilder;

public class BetTypeDAOImpl  extends AbstractDAO implements BetTypeDAO{
	
	public enum field {
		betid, betname, lockflag,createtime,updatetime,createip,updateip;
	}

	public BetTypeDAOImpl() throws ProjectXSystemException {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<BetTypeDTO> listAll() throws DataAccessException {
		// TODO Auto-generated method stub
		ResultSet rs = null;
		PreparedStatement ps = null;
		List<BetTypeDTO> list = null;
		
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get("ADM_BetTypeDAO_SELECT");
			String querySQL = _builder.getSql(null, null) ;
			ps = _builder.createStatement(_conn, querySQL, false, false);
			debugSql();
			rs = ps.executeQuery();
			list = toList(rs);
		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (null != rs) {
					rs.close();
				}
				if (null != ps) {
					ps.close();
				}
				if(null != _conn){
					_conn.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		return list;
	}

	private List<BetTypeDTO> toList(ResultSet rs) throws Exception{
		List<BetTypeDTO> list = new ArrayList<BetTypeDTO>();
		while (rs.next()) {
			String betid = getString(rs.getString(field.betid.ordinal()+1));
			String betname = getString(rs.getString(field.betname.ordinal()+1));
			int lockflag = rs.getInt(field.lockflag.ordinal()+1);
			String createtime = rs.getString(field.createtime.ordinal()+1);
			String updatetime = rs.getString(field.updatetime.ordinal()+1);
			String createip = getString(rs.getString(field.createip.ordinal()+1));
			String updateip = getString(rs.getString(field.updateip.ordinal()+1));
			BetTypeDTO dto = new BetTypeDTO();
			dto.setBetid(betid);
			dto.setBetname(betname);
			dto.setLockflag(lockflag);
			dto.setCreatetime(createtime);
			dto.setCreateip(createip);
			dto.setUpdatetime(updatetime);
			dto.setUpdateip(updateip);
			list.add(dto);
		}
		return list;
	}
}
